<template>
    <div style="width: 100%;height: 100%">
        <FormBoListPreview
            v-if="listAlias"
            ref="boList"
            :alias="listAlias"
            :params="params"
            @onCellClick="onCellClick"
        />
    </div>
</template>

<script>
import FormCustomPublic from "../FormCustomPublic";
import BusEvent from "../BusEvent";
import {defineAsyncComponent} from "vue";
import {Util} from "@lowcode/jpaas-base-lib"
export default {
    name: "listView",
    components: {
        FormBoListPreview:defineAsyncComponent(()=>import('@/views/modules/form/FormBoListPreview/FormBoListPreview.vue'))
    },
    mixins:[FormCustomPublic,BusEvent],
    data() {
        return {
            params:"",
            listAlias:"",
        }
    },
    created() {
        this.init();
        let self=this;
        if(!this.$bus.isFormEvent){
            this.$bus.on('formEvent',  (args)=> {
                args.eventKey = "formEvent";
                self.$bus.emit("formCustomViewEvent",args);
            });
            this.$bus.isFormEvent = true;
        }
    },
    beforeUnmount() {
        //销毁事件
        this.$bus.isFormEvent =false;
        this.$bus.off("formEvent");
    },
    methods: {
        getTable(){
            return this.$refs.boList.$refs.current.$refs.current;
        },
        getData() {
            return this.getTable().getTableObj().getData();
        },
        getSelectedRow() {
            return this.getTable().getTableData(true);
        },
        getSelectedRows() {
            return this.getTable().getTableData(false);
        },
        init(){
            if(this.config){
                //取路由参数
                let params=this.getRouterParams();
                let paramStr="";
                //读取配置的参数
                for (let i = 0; i < this.config.receive.mapping.length; i++) {
                    let o = this.config.receive.mapping[i];
                    if(o.valueSource == 'param'){
                        let val="";
                        if(this.config.receive.type== "url"){
                            if (params[o.valueDef]) {
                                val=params[o.valueDef];
                            }
                        }else {
                            if (params[o.name]) {
                                val= params[o.name];
                            }
                        }
                        //默认值
                        if(!val && o.defVal){
                            val=o.defVal;
                        }
                        paramStr += o.name + '=' + val + '&';
                    }
                }
                let self = this;
                this.getParams(this.config.receive.mapping,function (params) {
                    for (let key in params) {
                        paramStr += key + '=' + params[key] + '&';
                    }
                    self.params=paramStr;
                    self.listAlias=self.config.alias;
                });
            }
        },
        /**
         * 处理页面定制公共事件
         * @param args
         */
        handPublicReceive(args){
            //如果调用自定义的方法，则进行调用。
            if(args.method){
                this.$refs.boList.executeMethod(args.method,args.data);
                return ;
            }

            let self_=this;
            //通过自定义的事件
            if(args && args.type=="custom" && args.params){
                if(self_.$refs.boList){
                    self_.$refs.boList.handParams({},args.params);
                }
                return;
            }
            let receive = this.config.receive;
            let inputParams = args.params;
            let params = {};
            //读取配置的参数
            for (let i = 0; i < receive.mapping.length; i++) {
                let o = receive.mapping[i];
                //处理没有配置值来源的情况。
                if(inputParams[o.valueDef]){
                    params[o.name]=inputParams[o.valueDef];
                    if(o.values && o.values.length && inputParams[o.valueDef] && o.type=='rangepicker'){
                        let vl = inputParams[o.valueDef]
                        o.values.map((item,index)=>{
                            params[item] =vl[index]
                        })
                    }
                }
                else {
                    params[o.name]="";
                }
                //默认值
                if(!params[o.name] && o.defVal){
                    params[o.name]=o.defVal;
                }
            }
            this.getParams(receive.mapping,function (data) {
                if(data){
                    for (let key in data) {
                        params[key] = data[key];
                    }
                }
                if(self_.$refs.boList){
                    self_.$refs.boList.handParams({},params);
                }
            })
        },
        /**
         * 单元格被单击时会触发
         * @param row
         * @param rowIndex
         */
        onCellClick:Util.debounce(function ({row,rowIndex}){
            this.$bus.emit("formCustomViewEvent", {component: this.config.alias, params:row});
        },300),

    },
    watch: {
        config:{
            handler(val){
                this.init();
            },
            deep:true
        }
    },
}
</script>

<style scoped>

</style>
